import common from "../../../utils/public.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isremove: false,
    headerArray: [{
      text: '报备日期'
    },{
      text: '是否批准',
    }, {
      text: '外出时间'
    }, {
      text: '目的地'
    }, {
      text: '原因'
    }],
    reportList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = getApp().globalData.id
    console.log(id)
    wx.request({
      url: 'http://localhost:8080/clock_war_exploded/userReport/findById',
      method:'POST',
      header:{'content-type':'application/x-www-form-urlencoded;charset=utf-8'},
      data:{
        id:id
      },
      success: function(res) {
        if (res.data != null) {
          // for (var index = 0; index < res.data.length; index++) {
          //   res.data[index].date = common.getMyDate(res.data[index].date,'Y-m-d')
          // }
          res.data.forEach(item=>{
            item.date=common.getMyDate(item.date)
          })

          that.setData({
            searchLoading: true, //把"上拉加载"的变量设为true，显示  
            searchLoadingComplete: false, //把“没有数据”设为false，隐藏 
            reportList: res.data
          })
        } else {
          that.setData({
            searchLoadingComplete: true, //把“没有数据”设为true，显示  
            searchLoading: false //把"上拉加载"的变量设为false，隐藏  
          })
        }
      }
    })
  },

  formSubmit(e) {
    var that = this;
    var duration = e.detail.value.duration;
    var destination = e.detail.value.destination;
    var reason = e.detail.value.reason;
    var id = getApp().globalData.id;
    console.log('form发生了submit事件，携带数据为：', duration, destination, reason)
    var openid = getApp().globalData.openid;
    console.log('获取登录身份的唯一openid', openid)
    wx.request({
    url: 'http://localhost:8080/clock_war_exploded/userReport/save',
    method:'POST',
    header:{'content-type':'application/x-www-form-urlencoded;charset=utf-8'},
    data: {
      id: id,
      duration: duration,
      destination: destination,
      reason: reason
    },
    success: function (res) {
      if (res.data){
      wx.showToast({
        title: '提交成功',
        icon:'success',
      })
      wx.request({
        url: 'http://localhost:8080/clock_war_exploded/userReport/findById',
        method:'POST',
        header:{'content-type':'application/x-www-form-urlencoded;charset=utf-8'},
        data:{
          id:id
        },
        success: function(res) {
          if (res.data != null) {
            // for (var index = 0; index < res.data.length; index++) {
            //   res.data[index].date = common.getMyDate(res.data[index].date,'Y-m-d')
            // }
            res.data.forEach(item=>{
              item.date=common.getMyDate(item.date)
            })
  
            that.setData({
              searchLoading: true, //把"上拉加载"的变量设为true，显示  
              searchLoadingComplete: false, //把“没有数据”设为false，隐藏 
              reportList: res.data
            })
          } else {
            that.setData({
              searchLoadingComplete: true, //把“没有数据”设为true，显示  
              searchLoading: false //把"上拉加载"的变量设为false，隐藏  
            })
          }
        }
      })
      }else{
      wx.showModal({
        title: '温馨提示',
        content: '您已提交过，请不要重复提交',
      })
      }
              
    }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})